<?php
/**
 * @title 139. 单词拆分
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $s
     * @param String[] $wordDict
     * @return Boolean
     */
    function wordBreak($s, $wordDict) {
        /**
         * @since 2024-01-01 字典预处理
         * @since 2024-01-01 字典哈希匹配，长度数组
         */
        $this->wordHashArray = $this->wordLenArray = [];
        foreach ($wordDict as $word){
            $this->wordHashArray[$word] = true;
            $len = strlen($word);
            $this->wordLenArray[$len] = $len;
        }

        /**
         * @since 2024-01-01 长度排序
         */
        sort($this->wordLenArray);

        /**
         * @since 2024-01-01 全局字符串
         */
        $this->s = $s;

        /**
         * @since 2024-01-01 防止重复查询
         */
        $this->dpArray = [];


        /**
         * @return Boolean
         */
        return $this->WordConnect(0, strlen($s)-1);
    }

    /**
     * @title 字符串[s,e]区间内找字典匹配
     * @author start2004
     * @since 2024-01-01 Created
     *
     * @param Integer $s
     * @param Integer $e
     * @return Boolean
     */
    function WordConnect($s, $e){
        /**
         * @since 2024-01-01 是否存在缓存
         */
        if(isset($this->dpArray[$s])){
            return $this->dpArray[$s];
        } else {}

        if($s > $e){
            return $this->dpArray[$s] = true;
        } else {}

        /**
         * @since 2024-01-01 最大长度
         */
        $maxLength = $e-$s+1;

        /**
         * @since 2024-01-01 字典长度依次遍历
         */
        foreach ($this->wordLenArray as $len){
            /**
             * @since 2024-01-01 长度需要在范围内
             */
            if($len <= $maxLength){
                /**
                 * @since 2024-01-01 切len长度
                 */
                $word = substr($this->s, $s, $len);

                /**
                 * @since 2024-01-01 存在对应单词
                 */
                if(isset($this->wordHashArray[$word])){
                    /**
                     * @since 2024-01-01 继续查找
                     */
                    $connectResult = $this->WordConnect($s+$len, $e);
                    if($connectResult){
                        /**
                         * @return
                         */
                        return $this->dpArray[$s] = true;
                    } else {
                        /**
                         * @since 2024-01-01 继续匹配失败
                         */
                    }
                } else {
                    /**
                     * @since 2024-01-01 不存在单词
                     */
                }
            } else {
                /**
                 * @since 2024-01-01 长度过长
                 */
                break;
            }
        }

        /**
         * @return Boolean
         */
        return $this->dpArray[$s] = false;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.01_4.php
 */
$datas = [
    ["leetcode", ["leet", "code"]], // true
    ["catsandog", ["cats", "dog", "sand", "and", "cat"]], // false
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->wordBreak(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
